package com.erpanderp.wam.ac.view.reimbursement;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.selectonebutton.SelectOneButton;

import com.erpanderp.wam.ac.model.entity.project.ProjectAssignment;
import com.erpanderp.wam.ac.model.entity.reimbursement.ProjectPerdiem;
import com.erpanderp.wam.ac.model.entity.reimbursement.ProjectPerdiemDetail;
import com.erpanderp.wam.ac.model.service.project.ProjectAssignmentDAO;
import com.erpanderp.wam.ac.model.service.reimbursement.ProjectPerdiemDAO;
import com.erpanderp.wam.ac.model.service.reimbursement.ProjectPerdiemDetailDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;


@ManagedBean
@ViewScoped
public class ProjectPerdiemsBean {
	
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	private ProjectPerdiem projectPerdiem = new ProjectPerdiem();
	private String selectedButton = CommonLookup.CREATE_PROJECTPERDIEM_BUTTON;
	private List<ProjectAssignment> projectAssignmentList = new ArrayList<ProjectAssignment>();
	private boolean projecAssignmentsListRender;
	private ProjectAssignmentDAO projectAssignmentDAO = new ProjectAssignmentDAO();
	private WAMUser wamUser = new WAMUser();
	private boolean createPerdiemRequestForm;
	private boolean serchPerdiemForms;
	private List<ProjectPerdiem> projectPerdiemsList = new ArrayList<ProjectPerdiem>();
	private ProjectPerdiemDAO projectPerdiemDAO = new ProjectPerdiemDAO();
	private Long selectedPerdiemId;
	private boolean projectPerdiemsListRenderer;
	private String showRivision;
	private String revisionCount;
	private List<ProjectPerdiem> toBeRenderedPerdiemsList = new ArrayList<ProjectPerdiem>();
	private List<ProjectPerdiem> perdiemsListByPerdiemCode = new ArrayList<ProjectPerdiem>();
	private ProjectPerdiem perdiemByPerdiemCode = new ProjectPerdiem();
	private ProjectPerdiemDetail projectPerdiemDetail = new ProjectPerdiemDetail();
	private ProjectPerdiemDetailDAO projectPerdiemDetailDAO = new ProjectPerdiemDetailDAO();
	private boolean PROJECTASSIGNMENTSLIST_NOT_FOUND;
	private boolean PERDIEMLIST_NOT_FOUND;
	String revision;
	
	public ProjectPerdiemsBean(){
		
		wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
		if(wamUser != null){
			projectAssignmentList = projectAssignmentDAO.getAllProjectAssignments(wamUser.getEmployee().getEmployeeId());
			projectPerdiemsList = projectPerdiemDAO.getProjectPerdiemsByWamUserId(wamUser.getEmployee().getEmployeeId());
			if(projectPerdiemsList.size() > 0){
				for(ProjectPerdiem projectPerdiem : projectPerdiemsList){
					revisionCount = projectPerdiemDAO.getRevisionCountByPerdiemId(projectPerdiem.getProjectPerdiemId());
					revision = "R"+revisionCount;					
				//	projectPerdiemDetail = projectPerdiemDetailDAO.getProjectPerdiemDetailsByProjectPerdiemIdAndRevision(projectPerdiem.getProjectPerdiemId(), revision);
				}
			
		
	}
		}else{
		ValidationUtil.gotoLoginPage();
	}
		
		 if(projectAssignmentList != null && projectAssignmentList.size() > 0){
				
			   setProjecAssignmentsListRender(true);
		   }else{
			   PROJECTASSIGNMENTSLIST_NOT_FOUND = true;
		   }
		createPerdiemRequestForm = true;
		projectPerdiemsListRenderer = false;
		
	}
	
    public void buttonChangeListner(ActionEvent e){
    	 PROJECTASSIGNMENTSLIST_NOT_FOUND = false;
    	 PERDIEMLIST_NOT_FOUND = false;
		selectedButton = (((MenuItem)e.getComponent()).getValue().toString());
		System.out.println("BUTTON SELECTED -------->>>>>>"+selectedButton);
		if(selectedButton.equals(CommonLookup.EDIT_PROJECTPERDIEM_BUTTON)){
			createPerdiemRequestForm = false;	
			    if(projectPerdiemsList != null && projectPerdiemsList.size()>0){
			    	 PROJECTASSIGNMENTSLIST_NOT_FOUND = false;
			         setProjecAssignmentsListRender(false);
			         setProjectPerdiemsListRenderer(true);
			    }else{
			    	setProjectPerdiemsListRenderer(false);
			         	PERDIEMLIST_NOT_FOUND = true;
			    }
			    
			}else if(selectedButton.equals(CommonLookup.CREATE_PROJECTPERDIEM_BUTTON)){
				System.out.println("CREATE BUTTON TRIGGERED");
				 if(projectAssignmentList != null && projectAssignmentList.size() > 0){
					
					   setProjecAssignmentsListRender(true);
				   }else{
					   PROJECTASSIGNMENTSLIST_NOT_FOUND = true;
				   }
				createPerdiemRequestForm = true;
				projectPerdiemsListRenderer = false;
				/*FacesMessage msg = new FacesMessage("No perdiems", null);  
			    FacesContext.getCurrentInstance().addMessage(null, msg);*/
			}/*else if(selectedButton.equals(CommonLookup.EDIT_MSIC_BUTTON)){
				 if(projectPerdiemsList != null && projectPerdiemsList.size()>0){
					 System.out.println("EDIT BUTTON TRIGGERED");
					 createPerdiemRequestForm = false;
					 projectPerdiemsListRenderer = true;
			    	 PROJECTASSIGNMENTSLIST_NOT_FOUND = false;
			         setProjecAssignmentsListRender(false);
			    }else{
			    	PERDIEMLIST_NOT_FOUND = true;
			    }
				
				
			}*/else{
				System.out.println("VIEW BUTTON TRIGGERED");
				if(projectPerdiemsList != null && projectPerdiemsList.size()>0){
					PERDIEMLIST_NOT_FOUND = false;
			    	 PROJECTASSIGNMENTSLIST_NOT_FOUND = false;
			         setProjecAssignmentsListRender(false);
			         setProjectPerdiemsListRenderer(true);
			    }else{
			         	PERDIEMLIST_NOT_FOUND = true;
			    }
				createPerdiemRequestForm = false;
				
			}
			//selectedButton  = CommonLookup.EDIT_MSIC_BUTTON;
			//System.out.println("EDIT BUTTON SELECTED");

		
			
    	  // selectedButton = CommonLookup.VIEW_PRINT_MSIC_BUTTON;
		  
		  
		
		
	}
	
	 public String createProjectPerdiemFormAction() {
		 System.out.println("=====INSIDE createProjectPerdiemFormAction() method=======");
		 try{
		  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  
		  String projectAssignmentId = params.get("projectAssignmentId");
		  String employeeCode = params.get("employeeId");
		  
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("projectAssignmentId", projectAssignmentId);
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("employeeCode", employeeCode);		  
		  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/reimbursement/createprojectperdiem.html?projectAssignmentId="+projectAssignmentId+"&employeeCode="+employeeCode);
		  
		  System.out.println("travelCode ==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("projectAssignmentId"));
		  System.out.println("employeeCode==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("employeeCode"));
		  
		 
		  }catch (Exception e) {
			  System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
			  e.printStackTrace();
		 }
		 return "createprojectperdiem";
	 
		}
	 
	 public String editProjectPerdiemFormAction(){
		 System.out.println("=====INSIDE createProjectPerdiemFormAction() method=======");
		 try{
		  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  
		  String projectPerdiemCode = params.get("projectPerdiemCode");
		  String employeeId = params.get("employeeId");
		  String revision = params.get("revision");
		  String projectAssignmentId = params.get("projectAssignmentId");
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("projectPerdiemCode", projectPerdiemCode);
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("employeeCode", employeeId);		
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("revision", revision);	
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("projectAssignmentId", projectAssignmentId);	
		  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/reimbursement/editprojectperdiem.html?projectPerdiemCode="+projectPerdiemCode+"&employeeCode="+employeeId+"&revision="+revision+"&projectAssignmentId="+projectAssignmentId);
		  
		  System.out.println("travelCode ==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("projectAssignmentId"));
		  System.out.println("employeeCode==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("employeeCode"));
		  
		 
		  }catch (Exception e) {
			  System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
			  e.printStackTrace();
		 }
		 return "editprojectperdiem";
	 }
	 
	 public String viewAndPrintProjectPerdiemFormAction(){
		 System.out.println("=====INSIDE createProjectPerdiemFormAction() method=======");
		 try{
		  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  
		  String projectPerdiemCode = params.get("projectPerdiemCode");
		  String employeeId = params.get("employeeId");
		  String revision = params.get("revision");
		  String projectAssignmentId = params.get("projectAssignmentId");
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("projectPerdiemCode", projectPerdiemCode);
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("employeeCode", employeeId);		
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("revision", revision);	
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("projectAssignmentId", projectAssignmentId);	
		  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/reimbursement/viewprojectperdiem.html?projectPerdiemCode="+projectPerdiemCode+"&employeeCode="+employeeId+"&revision="+revision+"&projectAssignmentId="+projectAssignmentId);
		  
		  System.out.println("travelCode ==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("projectAssignmentId"));
		  System.out.println("employeeCode==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("employeeCode"));
		  
		 
		  }catch (Exception e) {
			  System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
			  e.printStackTrace();
		 }
		 return "editprojectperdiem";
	 }





	public List<ProjectAssignment> getProjectAssignmentList() {
		return projectAssignmentList;
	}




	public void setProjectAssignmentList(List<ProjectAssignment> projectAssignmentList) {
		this.projectAssignmentList = projectAssignmentList;
	}




	public ProjectPerdiem getProjectPerdiem() {
		return projectPerdiem;
	}




	public void setProjectPerdiem(ProjectPerdiem projectPerdiem) {
		this.projectPerdiem = projectPerdiem;
	}






	public String getSelectedButton() {
		return selectedButton;
	}




	public void setSelectedButton(String selectedButton) {
		this.selectedButton = selectedButton;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public boolean isProjecAssignmentsListRender() {
		return projecAssignmentsListRender;
	}

	public void setProjecAssignmentsListRender(boolean projecAssignmentsListRender) {
		this.projecAssignmentsListRender = projecAssignmentsListRender;
	}

	public boolean isCreatePerdiemRequestForm() {
		return createPerdiemRequestForm;
	}

	public void setCreatePerdiemRequestForm(boolean createPerdiemRequestForm) {
		this.createPerdiemRequestForm = createPerdiemRequestForm;
	}

	public boolean isSerchPerdiemForms() {
		return serchPerdiemForms;
	}

	public void setSerchPerdiemForms(boolean serchPerdiemForms) {
		this.serchPerdiemForms = serchPerdiemForms;
	}

	public List<ProjectPerdiem> getProjectPerdiemsList() {
		return projectPerdiemsList;
	}

	public void setProjectPerdiemsList(List<ProjectPerdiem> projectPerdiemsList) {
		this.projectPerdiemsList = projectPerdiemsList;
	}

	public Long getSelectedPerdiemId() {
		return selectedPerdiemId;
	}

	public void setSelectedPerdiemId(Long selectedPerdiemId) {
		this.selectedPerdiemId = selectedPerdiemId;
	}

	public boolean isProjectPerdiemsListRenderer() {
		return projectPerdiemsListRenderer;
	}

	public void setProjectPerdiemsListRenderer(boolean projectPerdiemsListRenderer) {
		this.projectPerdiemsListRenderer = projectPerdiemsListRenderer;
	}

	public String getShowRivision() {
		return showRivision;
	}

	public void setShowRivision(String showRivision) {
		this.showRivision = showRivision;
	}

	public List<ProjectPerdiem> getToBeRenderedPerdiemsList() {
		return toBeRenderedPerdiemsList;
	}

	public void setToBeRenderedPerdiemsList(List<ProjectPerdiem> toBeRenderedPerdiemsList) {
		this.toBeRenderedPerdiemsList = toBeRenderedPerdiemsList;
	}

	public List<ProjectPerdiem> getPerdiemsListByPerdiemCode() {
		return perdiemsListByPerdiemCode;
	}

	public void setPerdiemsListByPerdiemCode(
			List<ProjectPerdiem> perdiemsListByPerdiemCode) {
		this.perdiemsListByPerdiemCode = perdiemsListByPerdiemCode;
	}

	public ProjectPerdiem getPerdiemByPerdiemCode() {
		return perdiemByPerdiemCode;
	}

	public void setPerdiemByPerdiemCode(ProjectPerdiem perdiemByPerdiemCode) {
		this.perdiemByPerdiemCode = perdiemByPerdiemCode;
	}

	public String getRevisionCount() {
		return revisionCount;
	}

	public void setRevisionCount(String revisionCount) {
		this.revisionCount = revisionCount;
	}

	public ProjectPerdiemDetail getProjectPerdiemDetail() {
		return projectPerdiemDetail;
	}

	public void setProjectPerdiemDetail(ProjectPerdiemDetail projectPerdiemDetail) {
		this.projectPerdiemDetail = projectPerdiemDetail;
	}

	public String getRevision() {
		return revision;
	}

	public void setRevision(String revision) {
		this.revision = revision;
	}

	public boolean isPROJECTASSIGNMENTSLIST_NOT_FOUND() {
		return PROJECTASSIGNMENTSLIST_NOT_FOUND;
	}

	public void setPROJECTASSIGNMENTSLIST_NOT_FOUND(
			boolean pROJECTASSIGNMENTSLIST_NOT_FOUND) {
		PROJECTASSIGNMENTSLIST_NOT_FOUND = pROJECTASSIGNMENTSLIST_NOT_FOUND;
	}

	public boolean isPERDIEMLIST_NOT_FOUND() {
		return PERDIEMLIST_NOT_FOUND;
	}

	public void setPERDIEMLIST_NOT_FOUND(boolean pERDIEMLIST_NOT_FOUND) {
		PERDIEMLIST_NOT_FOUND = pERDIEMLIST_NOT_FOUND;
	}

}
